<?php
class Server {
  public static function autocomplete($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('term', '');
    
    $opts = new stdClass();
    $opts->cd = 't.track_id LIKE :v1';
    $opts->cdv = array(':v1' => '%' . $req . '%');
    $rslt = $orm->get('pb_colg_enrollment', 't.track_id', 'ASC', 1, SIZE, $opts)->d;
    $res = array();
    foreach ($rslt as $itm) {
      $obj = new stdClass();
      $obj->label = $itm->track_id;
      $obj->value = $itm->track_id;
      $res[] = $obj;
    }
    echo json_encode($res);
  }
  
  public static function autocomplete2($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('term', '');
    
    $opts = new stdClass();
    $opts->cd = 't.title__ LIKE :v1';
    $opts->cdv = array(':v1' => '%' . $req . '%');
    $rslt = $orm->get('pb_colg_student', 't.title__', 'ASC', 1, SIZE, $opts)->d;
    $res = array();
    foreach ($rslt as $itm) {
      $obj = new stdClass();
      $obj->label = $itm->title__;
      $obj->value = $itm->title__;
      $res[] = $obj;
    }
    echo json_encode($res);
  }
  
  public static function search($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.student_title__ LIKE :v2 AND t.is_trial = 0';
    $opts->cdv = array(':v2' => '%' . $req->term2 . '%');
    
    $opts->cd .= ' AND (';
    if ($req->done == 0 && $req->enrolled == 0 && $req->paid == 0 && $req->unpaid == 0) {
      $opts->cd .= 'false';
    } else if ($req->done == 1 && $req->enrolled == 0 && $req->paid == 0 && $req->unpaid == 0) {
      $opts->cd .= 't.is_current = 1';
    } else if ($req->done == 0 && $req->enrolled == 1 && $req->paid == 0 && $req->unpaid == 0) {
      $opts->cd .= 't.is_current = 0 AND t.is_enrolled = 1';
    } else if ($req->done == 0 && $req->enrolled == 0 && $req->paid == 1 && $req->unpaid == 0) {
      $opts->cd .= 't.is_current = 0 AND t.is_enrolled = 0 AND t.is_paid = 1';
    } else if ($req->done == 0 && $req->enrolled == 0 && $req->paid == 0 && $req->unpaid == 1) {
      $opts->cd .= 't.is_current = 0 AND t.is_enrolled = 0 AND t.is_paid = 0';
    } else if ($req->done == 1 && $req->enrolled == 1 && $req->paid == 0 && $req->unpaid == 0) {
      $opts->cd .= 't.is_current = 1 OR t.is_enrolled = 1';
    } else if ($req->done == 1 && $req->enrolled == 0 && $req->paid == 1 && $req->unpaid == 0) {
      $opts->cd .= '(t.is_current = 1) OR (t.is_paid = 1 AND t.is_enrolled = 0)';
    } else if ($req->done == 1 && $req->enrolled == 0 && $req->paid == 0 && $req->unpaid == 1) {
      $opts->cd .= '(t.is_current = 1) OR (t.is_paid = 0 AND t.is_enrolled = 0)';
    } else if ($req->done == 1 && $req->enrolled == 1 && $req->paid == 1 && $req->unpaid == 0) {
      $opts->cd .= '(t.is_current = 1 OR t.is_enrolled = 1) OR (t.is_paid = 1 AND t.is_enrolled = 0)';
    } else if ($req->done == 1 && $req->enrolled == 1 && $req->paid == 0 && $req->unpaid == 1) {
      $opts->cd .= '(t.is_current = 1 OR t.is_enrolled = 1) OR (t.is_paid = 0 AND t.is_enrolled = 0)';
    } else if ($req->done == 1 && $req->enrolled == 0 && $req->paid == 1 && $req->unpaid == 1) {
      $opts->cd .= '(t.is_current = 1) OR (t.is_enrolled = 0)';
    } else if ($req->done == 0 && $req->enrolled == 1 && $req->paid == 1 && $req->unpaid == 0) {
      $opts->cd .= '(t.is_current = 0 AND t.is_enrolled = 1) OR (t.is_paid = 1 AND t.is_enrolled = 0)';
    } else if ($req->done == 0 && $req->enrolled == 1 && $req->paid == 0 && $req->unpaid == 1) {
      $opts->cd .= '(t.is_current = 0 AND t.is_enrolled = 1) OR (t.is_paid = 0 AND t.is_enrolled = 0)';
    } else if ($req->done == 0 && $req->enrolled == 1 && $req->paid == 1 && $req->unpaid == 1) {
      $opts->cd .= '(t.is_current = 0 AND t.is_enrolled = 1) OR t.is_enrolled = 0';
    } else if ($req->done == 0 && $req->enrolled == 0 && $req->paid == 1 && $req->unpaid == 1) {
      $opts->cd .= 't.is_enrolled = 0';
    } else if ($req->done == 1 && $req->enrolled == 1 && $req->paid == 1 && $req->unpaid == 1) {
      $opts->cd .= 'true';
    }
    $opts->cd .= ')';

    $res = $orm->get('pb_colg_enrollment', $req->o, $req->q, $req->pg, SIZE, $opts);
    echo json_encode($res);
  }
}
?>